package com.sntk.registermanager.staff.application.controller;

import com.alibaba.fastjson.JSON;
import com.sntk.registermanager.common.infrastructure.resp.PageData;
import com.sntk.registermanager.common.infrastructure.resp.ResponseData;
import com.sntk.registermanager.common.infrastructure.session.LoginSessionInfo;
import com.sntk.registermanager.common.infrastructure.validation.Validation;
import com.sntk.registermanager.staff.application.dto.DoctorInfoCreator;
import com.sntk.registermanager.staff.application.dto.DoctorInfoUpdator;
import com.sntk.registermanager.staff.application.service.DoctorInfoService;
import com.sntk.registermanager.staff.query.DoctorInfoQuery;
import com.sntk.registermanager.staff.query.params.DoctorCondition;
import com.sntk.registermanager.staff.query.vo.DoctorInfoVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @author liuyunxia
 */
@RestController
@RequestMapping("/sinohis/register/external/staff/doctor")
@Api(value="DoctorInfoController",tags={"医生管理"})
public class DoctorInfoController {

    @Autowired
    private DoctorInfoService doctorInfoService;
    @Autowired
    private DoctorInfoQuery doctorInfoQuery;

    @PostMapping
    @ApiOperation(value="添加医生信息")
    public ResponseData addCareWorker(@ApiParam(name="creator",value="医生信息",required=true)
                                      @Validated @RequestBody DoctorInfoCreator creator,
                                      BindingResult result,
                                      HttpSession httpSession){

        List<?> l = Validation.getValidation(result);
        if(l.size() != 0) {
            return ResponseData.error(JSON.toJSONString(l));
        }
        doctorInfoService.addDoctor(creator, LoginSessionInfo.generate(httpSession).getOperatorInfo());
        return ResponseData.success();
    }

    @PutMapping("/{doctorCode}")
    @ApiOperation(value="修改医生信息")
    public ResponseData addCareWorker(@ApiParam(name="doctorCode",value="医生编号",required=true)
                                      @PathVariable String doctorCode,
                                      @ApiParam(name="updator",value="医生信息",required=true)
                                      @RequestBody @Validated DoctorInfoUpdator updator,
                                      BindingResult result,
                                      HttpSession httpSession){

        List<?> l = Validation.getValidation(result);
        if(l.size() != 0) {
            return ResponseData.error(JSON.toJSONString(l));
        }
        doctorInfoService.modify(doctorCode,updator,LoginSessionInfo.generate(httpSession).getOperatorInfo());
        return ResponseData.success();
    }

    @PutMapping("/disabled/{doctorCode}")
    @ApiOperation(value="医生禁用")
    public ResponseData disabled(@ApiParam(name="doctorCode",value="医生编号",required=true)
                                @PathVariable String doctorCode,
                                HttpSession httpSession){
        doctorInfoService.disabled(doctorCode,LoginSessionInfo.generate(httpSession).getOperatorInfo());
        return ResponseData.success();
    }

    @PutMapping("/enable/{doctorCode}")
    @ApiOperation(value="医生启用")
    public ResponseData enable(@ApiParam(name="doctorCode",value="医生编号",required=true)
                               @PathVariable String doctorCode,
                               HttpSession httpSession){
        doctorInfoService.enable(doctorCode,LoginSessionInfo.generate(httpSession).getOperatorInfo());
        return ResponseData.success();
    }

    @PutMapping("/modityimg/{doctorCode}")
    @ApiOperation(value="修改照片")
    public ResponseData modityDoctorImg(@ApiParam(name="doctorCode",value="医生编号",required=true)
                                        @PathVariable String doctorCode,
                                        @ApiParam(name="file",value="医生照片",required=true)
                                                MultipartFile file,
                                        HttpSession httpSession){
        doctorInfoService.modityDoctorImg(doctorCode, file);
        return ResponseData.success();
    }


    @GetMapping("/page/condition")
    @ApiOperation(value="分页条件查询医生信息")
    public ResponseData<PageData<DoctorInfoVO>> searchDoctorOfCondition(@ApiParam(name="condition",value="分页查询条件",required=true)
                                                                        DoctorCondition condition,
                                                                        HttpSession httpSession){

        PageData<DoctorInfoVO> vos = doctorInfoQuery.searchDoctorOfCondition(condition,LoginSessionInfo.generate(httpSession).getOperatorInfo());
        return ResponseData.success().setData(vos);
    }

}
